package com.softeem.yuedong_server.controller;

import com.softeem.yuedong_server.dto.Result;
import com.softeem.yuedong_server.utils.QiniuOssUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

public class BaseController {

    /*
    * 上传文件到oss中，返回文件地址*/
    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestPart MultipartFile file) throws IOException {
        //文件输入流
        InputStream inputStream = file.getInputStream();
        //生产一个随机字符串作为文件的key
        String key = UUID.randomUUID().toString();

        //创建OSS工具对象
        QiniuOssUtils oss = new QiniuOssUtils();
        //上传
        String url = oss.upload(inputStream,key);
        return url != null ? Result.ok().setData(url).setMsg("上传成功") : Result.fail().setMsg("上传失败");
    }
}
